package com.gz.controller;

import com.alibaba.fastjson.JSON;
import com.gz.common.CouponService;
import com.gz.common.CustomerService;
import com.gz.common.model.Coupon;
import com.gz.common.model.Customer;
import com.gz.utils.Response;
import com.gz.utils.StringUtil;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Redis;

/**
 * Created by gongzhen on 2018/6/2.
 */
public class CouponController extends Controller {

    public void getCoupon() {
        Coupon coupon = CouponService.getService().getCoupon();
        renderJson(Response.responseJson(200, "成功", coupon));
    }

    public void getNewUserCoupon() {
        String token = this.getHeader("token");
        System.out.printf(token);
        Coupon coupon =null;
        if(token!=null) {
            String customerJson = Redis.use("cms").get(token);
            if (StringUtil.isEmpty(customerJson)) {

            } else {
                Customer customer = JSON.parseObject(customerJson, Customer.class);
                customer=CustomerService.getService().getCustomerByAccount(customer.getMobile());
//                if(customer.getIsFirst().equals(0)){
                    coupon = CouponService.getService().getNewUserCoupon();
                    CustomerService.getService().updateIsFirst(customer);
//                }
            }
        }
        renderJson(Response.responseJson(200, "成功", coupon));
    }
}
